﻿using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ClassLibrary {
    public partial class GradientBox : Control {
        public GradientBox() {
            InitializeComponent();
        }

        private Color _color1;
        public Color Color1 {
            get { return _color1; }
            set { 
                _color1 = value;
                Invalidate();
            }
        }

        private Color _color2;
        public Color Color2 {
            get { return _color2; }
            set { 
                _color2 = value;
                Invalidate();
            }
        }
        private LinearGradientMode _linearGradientMode;
        public LinearGradientMode LinearGradientMode {
            get { return _linearGradientMode; }
            set { 
                _linearGradientMode = value;
                Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs pe) {
            base.OnPaint(pe);
            PaintGradientColor(this, Color1, Color2, LinearGradientMode);
        }

        private void PaintGradientColor(Control control, Color color1, Color color2, LinearGradientMode mode) {
            LinearGradientBrush brush = new LinearGradientBrush(new RectangleF(0, 0, control.Width, control.Height), color1, color2, mode);
            Graphics frmGraphics = control.CreateGraphics();
            frmGraphics.FillRectangle(brush, new RectangleF(0, 0, control.Width, control.Height));
            frmGraphics.Dispose();
        }

    }
}
